using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.CommonWeb;
using System.IO;
using NTTHAO.Web.BL.Product;

public partial class admin_ProductContent : System.Web.UI.Page
{
    public bool _ReloadTop = false;
    public String _ReloadQuery = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        LanguageUtility.CheckLanguage(Request, Response, Session);

        if (!Context.User.Identity.IsAuthenticated)
        {
            this.divMain.InnerHtml = "<a href=\"#\" onclick=\"top.location.href='login.aspx?ReturnUrl=' + encodeURIComponent(top.location.href);\">" + LanguageUtility.GetTrueLanguageText("LOGIN_REQUIRED", Session) + "</a>";
            return;
        }
        
        try
        {
            string control = String.Empty;
            string action = Request.QueryString["action"].Trim();

            if ("view".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/Product/ProductTypeContent.ascx";
            }
            else if ("config".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/Product/ProductTypeConfig.ascx";
            }
            else if ("mantype".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/Product/ProductTypeManagement.ascx";
            }
            else if ("addtype".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/Product/ProductTypeInput.ascx";
            }
            else if ("edittype".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/Product/ProductTypeInput.ascx";
            }
            else if ("deletetype".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                int typeID = Int32.Parse(Request.QueryString["typeid"].Trim());
                this.DeleteProductType(typeID);

                control = "webparts/Common/Blank.ascx";
                _ReloadTop = true;
            }
            else if ("movetype".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/Product/ProductTypeMove.ascx";
            }
            else if ("typemoveup".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    int typeID = Int32.Parse(Request.QueryString["typeid"].Trim());
                    this.MoveTypeUp(typeID);
                }
                catch (Exception) { }

                Response.Redirect("ProductContent.aspx?action=mantype&g=" + Request.QueryString["g"] + "&typeid=" + Request.QueryString["parentid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
                return;
            }
            else if ("typemovedown".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    int typeID = Int32.Parse(Request.QueryString["typeid"].Trim());
                    this.MoveTypeDown(typeID);
                }
                catch (Exception) { }

                Response.Redirect("ProductContent.aspx?action=mantype&g=" + Request.QueryString["g"] + "&typeid=" + Request.QueryString["parentid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
                return;
            }
            else if ("procopy".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/Product/ProductCopy.ascx";
            }
            else if ("proadd".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/Product/ProductInput.ascx";
            }
            else if ("proedit".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Plug-ins/Product/ProductInput.ascx";
            }
            else if ("prodel".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    int productID = Int32.Parse(Request.QueryString["id"].Trim());
                    this.DeleteProduct(productID);
                }
                catch (Exception) { }

                Response.Redirect("ProductContent.aspx?action=view&g=" + Request.QueryString["g"] + "&typeid=" + Request.QueryString["typeid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
                return;
            }
            else if ("promoveup".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    int productID = Int32.Parse(Request.QueryString["id"].Trim());
                    this.MoveProductUp(productID);
                }
                catch (Exception) { }

                Response.Redirect("ProductContent.aspx?action=view&g=" + Request.QueryString["g"] + "&typeid=" + Request.QueryString["typeid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
                return;
            }
            else if ("promovedown".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    int productID = Int32.Parse(Request.QueryString["id"].Trim());
                    this.MoveProductDown(productID);
                }
                catch (Exception) { }

                Response.Redirect("ProductContent.aspx?action=view&g=" + Request.QueryString["g"] + "&typeid=" + Request.QueryString["typeid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
                return;
            }
            else if ("err".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                string error = Request.QueryString["err"];
                if ("notauthorised".Equals(error, StringComparison.OrdinalIgnoreCase))
                {
                    control = "webparts/Common/NotAuthorised.ascx";
                }
                else
                {
                    control = "webparts/Common/InvalidURL.ascx";
                }
            }
            else if ("reloadtop".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                control = "webparts/Common/Blank.ascx";
                if (Request.QueryString["typeid"] != null)
                {
                    _ReloadQuery = "typeid=" + Request.QueryString["typeid"];
                }
                _ReloadTop = true;
            }

            if (control == String.Empty)
            {
                control = "webparts/Common/InvalidURL.ascx";
            }

            if (control != String.Empty)
            {
                this.divMain.Controls.Add(LoadControl(control));
            }
        }
        catch (Exception ex) 
        {
            HtmlGenericControl ctrl = new HtmlGenericControl("div");
            ctrl.InnerHtml = string.Format("<p>{0}</p><p>{1}</p>", ex.Message, ex.StackTrace);
            this.divMain.Controls.Add(ctrl);
        }
    }

    public void DeleteProductType(int typeID)
    {
        try
        {
            NTTHAO.Web.BL.Product.ProductType productType = new NTTHAO.Web.BL.Product.ProductType(typeID);
            if (productType.ID > 0)
            {
                DataSet ds = ProductType.GetAllByParent(productType.ID);
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        int childID = (int)row[ProductType.ID__COLUMN_NAME];
                        this.DeleteProductType(childID);
                    }
                }

                NTTHAO.Web.BL.Product.ProductTypeConfig productTypeConfig = NTTHAO.Web.BL.Product.ProductTypeConfig.GetConfigByType(productType.GroupID, typeID);
                if (productTypeConfig.ID > 0)
                {
                    productTypeConfig.Delete();
                }
                productType.Delete();

                string filePath = "../" + Constant.UPLOAD_FILE_PATH + "product/type/" + productType.ID.ToString() + "/";
                WebGlobal.EmptyVirtualFolder(filePath, false);
            }
        }
        catch (Exception)
        {
        }
    }

    public void DeleteProduct(int productID)
    {
        try
        {
            NTTHAO.Web.BL.Product.Product product = new NTTHAO.Web.BL.Product.Product(productID);
            if (product.ID > 0)
            {
                product.Delete();
                string filePath = "../" + Constant.UPLOAD_FILE_PATH + product.ImagePath;
                WebGlobal.EmptyVirtualFolder(filePath, false);
            }
        }
        catch (Exception)
        {
        }
    }

    private void MoveProductUp(int productID)
    {
        try
        {
            NTTHAO.Web.BL.Product.Product product = new NTTHAO.Web.BL.Product.Product(productID);
            product.MoveUp();
        }
        catch (Exception) { }
    }

    private void MoveProductDown(int productID)
    {
        try
        {
            NTTHAO.Web.BL.Product.Product product = new NTTHAO.Web.BL.Product.Product(productID);
            product.MoveDown();
        }
        catch (Exception) { }
    }

    private void MoveTypeUp(int typeID)
    {
        try
        {
            NTTHAO.Web.BL.Product.ProductType type = new NTTHAO.Web.BL.Product.ProductType(typeID);
            type.MoveUp();
        }
        catch (Exception) { }
    }

    private void MoveTypeDown(int typeID)
    {
        try
        {
            NTTHAO.Web.BL.Product.ProductType type = new NTTHAO.Web.BL.Product.ProductType(typeID);
            type.MoveDown();
        }
        catch (Exception) { }
    }
}
